package com.xm.dao.impl;

import com.xm.dao.CartDao;
import com.xm.po.Cart;
import com.xm.util.BaseDao;
import com.xm.util.DButil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 * @date 2020-08-07-15:06
 * @description 购物车数据操作接口实现类
 */
public class CartDaoImpl implements CartDao {
    @Override
    public boolean add(Cart cart) {
        return DButil.insert(cart);
    }

    @Override
    public Cart getCartById(Integer goodsId, Integer userId) {
        Connection conn = DButil.getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;
        Cart cart = new Cart();
        String sql = "select * from cart where goodsId = ? and userId = ?";
        try {
            ps = conn.prepareStatement(sql);
            ps.setObject(1,goodsId);
            ps.setObject(2,userId);
            rs = ps.executeQuery();
            cart = (Cart) DButil.getRsOne(Cart.class,rs);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DButil.close(rs,ps,conn);
        }
        return cart;
    }

    @Override
    public List<Cart> getAll(Integer userId) {
        List<Cart> cart = new ArrayList<>();
        Connection conn = DButil.getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql = "select * from cart where userId = ?";
        try {
            ps = conn.prepareStatement(sql);
            ps.setObject(1,userId);
            rs = ps.executeQuery();
            cart = (List<Cart>) DButil.getRsData(Cart.class,rs);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DButil.close(rs,ps,conn);
        }
        return cart;
    }

    @Override
    public List<Cart> getCartByIdChecked(Integer userId) {
        List<Cart> cart = new ArrayList<>();
        Connection conn = DButil.getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql = "select * from cart where userId = ? and isCheck = 1";
        try {
            ps = conn.prepareStatement(sql);
            ps.setObject(1,userId);
            rs = ps.executeQuery();
            cart = (List<Cart>) DButil.getRsData(Cart.class,rs);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DButil.close(rs,ps,conn);
        }
        return cart;
    }

    @Override
    public boolean update(Cart cart) {
        return DButil.update(cart);
    }

    @Override
    public boolean delete(Integer goodsId, Integer userId)  {
        Connection conn = DButil.getConnection();
        PreparedStatement ps = null;
        String sql = "delete from cart where goodsId = ? and userId = ?";
        try {
            ps = conn.prepareStatement(sql);
            ps.setObject(1,goodsId);
            ps.setObject(2,userId);
            return ps.executeUpdate()>0;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DButil.close(null,ps,conn);
        }
        return false;
    }

    @Override
    public boolean delete(Integer userId) {
        return false;
    }

    @Override
    public boolean isCheck(Integer goodsId, Integer userId, Integer isCheck) {
        Connection conn = DButil.getConnection();
        PreparedStatement ps = null;
        String sql = "update cart set isCheck = ? where goodsId = ? and userId = ?";
        try {
            ps = conn.prepareStatement(sql);
            ps.setObject(1,isCheck);
            ps.setObject(2,goodsId);
            ps.setObject(3,userId);
            return ps.executeUpdate()>0;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DButil.close(null,ps,conn);
        }
        return false;
    }

    @Override
    public List<Cart> getAll() {
        return (List<Cart>) DButil.queryAll(Cart.class);
    }

    @Override
    public boolean deleteCart(Integer cartId) {
        Connection conn = DButil.getConnection();
        PreparedStatement ps = null;
        String sql = "delete from cart where cartId = ? ";
        try {
            ps = conn.prepareStatement(sql);
            ps.setObject(1,cartId);
            return ps.executeUpdate()>0;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DButil.close(null,ps,conn);
        }
        return false;
    }

    @Override
    public Cart findInfo(Integer cartId) {
        Connection conn = DButil.getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;
        Cart cart = new Cart();
        String sql = "select * from cart where cartId = ?";
        try {
            ps = conn.prepareStatement(sql);
            ps.setObject(1,cartId);
            rs = ps.executeQuery();
            cart = (Cart) DButil.getRsOne(Cart.class,rs);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DButil.close(rs,ps,conn);
        }
        return cart;
    }
}
